package com.example.springbootfoodsafety.util;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Component
public class UploadUtil {
    public static String upload(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        //获取文件名
        String fileName = file.getOriginalFilename();
        fileName=UuId.getUUID()+GetSuffix.getSuffix(fileName);
//        存储路径
        String filePath = "D:/JavaPrejcet/SpringBootFoodSafety/src/main/resources/static/uploadFile/";
        File dir = new File(filePath + fileName);
        if (!dir.isDirectory()) {//文件目录不存在，就创建一个
            dir.mkdirs();
        }
        try {
            file.transferTo(dir);
            return fileName;
        } catch (IOException e) {
            e.printStackTrace();
//            System.out.println("error");
        }
        return "上传失败！";
    }
}
